package com.ajax;

import com.ajax.pojo.Student;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;

/**
 * @author wzm
 * @create 2023-11-22 15:17
 * project_name:web2310
 * @Description
 */
@WebServlet("/hello3")
public class AxiosServlet3 extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //处理编码
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/json;charset=UTF-8");
        
        //尝试使用通用的方法获取 null
        //ajax提交的post请求无法直接获得请求体中的数据 需要采用post特有的方式获取
        /*String name = req.getParameter("name");
        System.out.println("name = " + name)*/;

        BufferedReader reader = req.getReader();
        String s = reader.readLine(); //String

        //如果新增  需要java对象转换  == 阿里巴巴fastjson
        
        //String = java
        Student student = JSON.parseObject(s, Student.class);
        System.out.println("student = " + student);
        
        //有的时候 我们假如是查询 需要返回字符串给前端 但是我们得到的是java
        //java=String
        String s1 = JSON.toJSONString(student);
//        resp.getWriter().write(s1);


    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       doPost(req, resp);
    }
}
